package com.hula.onion.book.server.spider.platforms.kesu;

import com.alibaba.fastjson.JSONObject;
import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.common.LoginForm;
import com.hula.onion.book.server.config.ResponseStatusEnum;
import com.hula.onion.book.server.model.AccountInfo;
import com.hula.onion.book.server.utils.DateUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Description:
 * Create Date:2018/1/30 19:49
 *
 * @author liangbing
 * @version 1.0
 */
@Component
public class KeSuSpiderHelper {

    /**
     * 组装请求参数
     *
     * @param form
     * @return
     */
    public Map<String, Object> installRequestParams(LoginForm form) {

        Map<String, Object> map = new HashMap<>(3);

        map.put("loginname", form.getUserName());
        map.put("loginpass", form.getPassword());
        map.put("code_num", form.getTicket());

        return map;

    }

    /**
     * 组装登陆数据参数
     *
     * @return
     */
    public Map<String, Object> installLoginParams() {
        Map<String, Object> param = new HashMap<>(3);
        param.put("method", "accountInfo");
        param.put("channelType", "PC");
        param.put("subtime", DateUtils.getCurrentMillisecond());
        return param;
    }

    /**
     * 解析账户余额
     *
     * @param html
     * @return
     */
    public CommonResponse parseAccountBalance(String html) {
        System.out.println(html);
        try {
            AccountInfo accountInfo = new AccountInfo();
            int status = JSONObject.parseObject(html).getInteger("status");
            if (200 == status) {
                String amount = JSONObject.parseObject(html).getJSONObject("result").getString("totalAmount");
                if (!StringUtils.isEmpty(amount)) {
                    accountInfo.setAmount(new BigDecimal(amount));
                }
            }
            return new CommonResponse(accountInfo);
        } catch (Exception e) {
            return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
        }
    }
}
